package com.sgb.kjwl.view.ui.activity.manage

import android.graphics.Rect
import android.os.Bundle
import android.view.View
import android.view.Window
import android.widget.LinearLayout
import android.widget.RelativeLayout
import com.blankj.utilcode.util.SizeUtils
import com.gyf.barlibrary.ImmersionBar
import com.sgb.kjwl.AppActivity
import com.sgb.kjwl.R

open class WebSiteBuildBaseActivity : AppActivity(){

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        //解决软键盘挡住recycleView输入框的问题
        val decorView = window.decorView
        val contentView = findViewById<View>(Window.ID_ANDROID_CONTENT)
        val rootView = contentView.findViewById<View>(R.id.ns_scroll_view)
        var lpRl : RelativeLayout.LayoutParams ?= null
        var lpLl : LinearLayout.LayoutParams ?= null
        if (rootView.layoutParams is RelativeLayout.LayoutParams){
            lpRl = rootView.layoutParams as RelativeLayout.LayoutParams
        } else if (rootView.layoutParams is LinearLayout.LayoutParams ){
            lpLl = rootView.layoutParams as LinearLayout.LayoutParams
        }
        decorView.viewTreeObserver.addOnGlobalLayoutListener {
            val r = Rect()
            decorView.getWindowVisibleDisplayFrame(r)
            val height = decorView.resources.displayMetrics.heightPixels
            val diff = height - r.bottom
            if (diff != 0) {
                if (contentView.paddingBottom != diff) {
                    if (lpRl != null){
                        lpRl.bottomMargin = Math.max(
                            0,
                            diff + ImmersionBar.getStatusBarHeight(this) - SizeUtils.dp2px(
                                52f
                            )
                        )
                    } else if (lpLl != null){
                        lpLl.bottomMargin = Math.max(
                            0,
                            diff + ImmersionBar.getStatusBarHeight(this) - SizeUtils.dp2px(
                                52f
                            )
                        )
                    }
                }
            } else {
                if (contentView.paddingBottom != 0) {
                    if (lpRl != null){
                        lpRl.bottomMargin = 0
                    } else if (lpLl != null){
                        lpLl.bottomMargin = 0
                    }
                }
            }
            if (lpRl != null){
                rootView.layoutParams = lpRl
            } else if (lpLl != null){
                rootView.layoutParams = lpLl
            }
        }
        ImmersionBar.with(this)
            .statusBarDarkFont(statusBarDark())
            .fitsSystemWindows(false)
            .keyboardEnable(false).init()

    }
}